package viettravel.common;

import java.io.ByteArrayOutputStream;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.regex.Pattern;
import org.holoeverywhere.drawable.ColorDrawable;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.facebook.model.GraphUser;
import com.google.android.gms.maps.model.LatLng;
import viettravel.DAO.PlaceDAO;
import viettravel.guiandevent.R;

@SuppressLint({ "SdCardPath", "DefaultLocale" })
public class Constants {
	public static final Integer CONNECTION_TIMEOUT = 5000;
	public static final Integer SOCKET_TIMEOUT = 5000;

	// Slide image properties
	public static final int SLIDE_IMAGE_WIDTH = 150;
	public static final int SLIDE_IMAGE_HEIGHT = 100;

	public static final int DISTANCE = 20;

	// Url Root
	public static final String ROOT = "http://120.72.85.119:8080/ViettravelWS/webresources/";
	public static final String SUFFIX_GET_REGIONS = "regions/getRegions";
	public static final String SUFFIX_GET_RPOVINCES = "provinces/getProvinces";
	public static final String SUFFIX_GET_PLACES = "places/getPlaces";
	public static final String SUFFIX_GET_ACTIVITIES = "activities/getActivities";
	public static final String SUFFIX_GET_TOURISTFORMS = "touristforms/getTours";
	public static final String SUFFIX_GET_SPECIALTIESS = "specialties/getSpecialties";
	public static final String SUFFIX_GET_IMAGES = "images/getImagesByPlaceID/";
	public static final String SUFFIX_GET_COMMENTS = "comments/getComments/";
	public static final String SUFFIX_GET_COMMENTBYPLACEID = "comments/getCommentsByPlaceId/";
	public static final String SUFFIX_GET_FAVORITEPLACE = "places/getFavouritPlaces/";

	public static final String SUFFIX_POST_PLACES = "places/addPlace";
	public static final String SUFFIX_POST_FAVORITEPLACE = "places/addFavouritPlace";
	public static final String SUFFIX_POST_USER = "users/addUser";
	public static final String SUFFIX_POST_COMMENT = "comments/addComment";
	public static final String SUFFIX_POST_RATE = "places/rate";

	// Server Data
	// REGION DATA
	public static final String TAG_REGIONS = "regions";
	public static final String TAG_REGION_ID = "regionId";
	public static final String TAG_REGION_NAME = "regionName";
	public static final String TAG_REGION_DESCRIPTION = "regionDescription";

	// PROVINCE DATA
	public static final String TAG_PROVINCES = "provinces";
	public static final String TAG_PROVINCE_ID = "provinceId";
	public static final String TAG_PROVINCE_NAME = "provinceName";
	public static final String TAG_PROVINCE_DESCRIPTION = "provinceDescription";

	// PLACE DATA
	public static final String TAG_PLACES = "places";
	public static final String TAG_PLACES_ID = "placeId";
	public static final String TAG_PLACES_NAME = "placeName";
	public static final String TAG_PLACES_DESCIPTION = "placeDescription";
	public static final String TAG_PLACES_LONGTITUDE = "placeLong";
	public static final String TAG_PLACES_LATITUDE = "placeLat";
	public static final String TAG_PLACES_RATING = "rating";
	public static final String TAG_PLACES_TOTAL_RATE = "totalRate";

	// ACTIVITY DATA
	public static final String TAG_ACTIVITIES = "activities";
	public static final String TAG_ACTIVITIES_ID = "activityId";
	public static final String TAG_ACTIVITIES_NAME = "activityName";
	public static final String TAG_ACTIVITIES_NOTE = "activityNote";

	// TOURISTFORM DATA
	public static final String TAG_TOURISTFORMS = "touristForms";
	public static final String TAG_TOURISTFORMS_ID = "tourId";
	public static final String TAG_TOURISTFORMS_NAME = "tourName";
	public static final String TAG_TOURISTFORMS_NOTE = "tourNote";

	// SPECIALTIES DATA
	public static final String TAG_SPECIALTIES = "specialties";
	public static final String TAG_SPECIALTIES_ID = "specialtyId";
	public static final String TAG_SPECIALTIES_NAME = "specialtyName";
	public static final String TAG_SPECIALTIES_NOTE = "specialtyNote";

	// USER DATA
	public static final String TAG_USERS = "users";
	public static final String TAG_USERS_ID = "userId";
	public static final String TAG_USERS_NAME = "userName";

	// COMMENTS DATA
	public static final String TAG_COMMENTS = "comments";
	public static final String TAG_COMMENTS_ID = "cmtId";
	public static final String TAG_COMMENTS_TITLE = "cmtTitle";
	public static final String TAG_COMMENTS_IMAGE = "cmtImage";
	public static final String TAG_COMMENTS_CONTENT = "cmtContent";
	public static final String TAG_COMMENTS_DATE = "cmtDt";
	public static final String TAG_COMMENTS_SENDER = "cmtSender";
	public static final String TAG_COMMENTS_PAGE = "page";
	public static final String TAG_COMMENTS_PERPAGE = "perPage";
	public static final String TAG_COMMENTS_TOTAL = "total";
	public static final String TAG_COMMENTS_TOTALPAGE = "totalPage";

	// IMAGE DATA
	public static final String TAG_IMAGES = "images";
	public static final String TAG_IMAGES_ID = "imgId";
	public static final String TAG_IMAGES_IMAGE = "imgBin";
	public static final String TAG_IMAGES_CAPTION = "imgCaption";

	public static final String TAG_RESULT = "result";
	public static final String TAG_RATE_SCORE = "score";

	// LOG
	public static final String TAG = "MYLOG";

	// Context
	public static Context CONTEXT = null;

	public static int THEME = R.style.Theme_Sherlock_Light;
	public static Drawable APP_DRAWABLE = new ColorDrawable(
			Color.parseColor("#78d34c"));
	public static GraphUser CURRENT_FACEBOOK_USER = null;
	public static String DATABASE_CONNECTION_STRING = "";
	public static String SERVER_SERVICE_URL = "";
	public static String APP_VERSION = "1.0.0";
	public static String WEBSITE = "http://vi-travel.info";
	public static Typeface TYPEFACE_CAVIAR;// =
											// Typeface.createFromAsset(CONTEXT.getAssets(),"fonts/utm_caviar.ttf");
	public static Typeface TYPEFACE_NYALA;// =
											// Typeface.createFromAsset(CONTEXT.getAssets(),"fonts/utm_nyala.ttf");

	public static ArrayList<PlaceDAO> CURRENT_LIST_SEARCH;
	/*
	 * PutExtra KEY
	 */
	public static String EXTRA_PROVINCE_ID = "EXTRA_PROVINCE_ID";
	public static String EXTRA_PLACE_ID = "EXTRA_PLACE_ID";
	public static String EXTRA_PLACE_NAME = "EXTRA_PLACE_NAME";
	public static String EXTRA_PLACE_LONG = "EXTRA_PLACE_LONG";
	public static String EXTRA_PLACE_LAT = "EXTRA_PLACE_LAT";
	public static int RESULT_LIST_CHECKNAME = 10;
	public static String EXTRA_NEWPLACE_CHECKNAME_PLACENAME = "EXTRA_NEWPLACE_CHECKNAME_PLACENAME";
	public static String EXTRA_NEWPLACE_CHECKNAME_PLACELAT = "EXTRA_NEWPLACE_CHECKNAME_PLACELAT";
	public static String EXTRA_NEWPLACE_CHECKNAME_PLACELONG = "EXTRA_NEWPLACE_CHECKNAME_PLACELONG";
	public static String EXTRA_NEWPLACE_CHECKNAME_VALUE = "EXTRA_NEWPLACE_CHECKNAME_VALUE";
	public static String EXTRA_GETFRIEND = "EXTRA_GETFRIEND";
	// Facebook TAG
	public static String TAG_FB_DATA = "data";
	public static String TAG_FB_CURRENT_LOCATION = "current_location";
	public static String TAG_FB_LONGTITUTE = "longitude";
	public static String TAG_FB_LATITUE = "latitude";
	public static String TAG_FB_PIC_SQU = "pic_square";
	public static String TAG_FB_NAME = "name";
	/*
	 * Client Database
	 */

	public static String CLIENT_DATA_FOLDER_PATH = "/data/data/viettravel.guiandevent/databases/";
	public static String CLIENT_DATA_NAME = "vitravel.db";
	public static int CLIENT_VERSION = 1;

	// REGIONS
	public static String REGION_TABLE_NAME = "REGIONS";
	public static String REGION_FIELD_ID_NAME = "Region_Id";
	public static String REGION_FIELD_REGIONNAME_NAME = "Region_Name";
	public static String REGION_FIELD_REGIONDESCRIPTION_NAME = "Region_Description";
	public static String QUERY_GET_REGION_BY_ID = "SELECT * FROM "
			+ REGION_TABLE_NAME + " WHERE " + REGION_FIELD_ID_NAME + " = '%s'";
	public static String QUERY_GET_ALL_REGIONS = "SELECT * FROM "
			+ REGION_TABLE_NAME;
	public static String QUERY_INSERT_REGIONS = "INSERT OR REPLACE INTO "
			+ REGION_TABLE_NAME + "(" + REGION_FIELD_ID_NAME + ","
			+ REGION_FIELD_REGIONNAME_NAME + ","
			+ REGION_FIELD_REGIONDESCRIPTION_NAME
			+ ") VALUES ( %s , '%s' , '%s')";

	// PROVINCES
	public static String PROVINCE_TABLE_NAME = "PROVINCES";
	public static String PROVINCE_FIELD_ID = "Province_Id";
	public static String PROVINCE_FIELD_NAME = "Province_Name";
	public static String PROVINCE_FIELD_DESCRIPTION = "Province_Description";
	public static String PROVINCE_FIELD_REGION_ID = "Region_Id";
	public static String QUERY_GET_PROVINCE_BY_ID = "SELECT * FROM "
			+ PROVINCE_TABLE_NAME + " WHERE " + PROVINCE_FIELD_ID + " = '%s'";
	public static String QUERY_GET_PROVINCES_BY_REGION_ID = "SELECT * FROM "
			+ PROVINCE_TABLE_NAME + " WHERE " + PROVINCE_FIELD_REGION_ID
			+ " = '%s'" + "ORDER BY " + PROVINCE_FIELD_NAME + " ASC";// AND
																		// " + PROVINCE_FIELD_NAME + "
																		// LIKE
																		// %s";

	public static String QUERY_INSERT_PROVINCES = "INSERT OR REPLACE INTO "
			+ PROVINCE_TABLE_NAME + "(" + PROVINCE_FIELD_ID + ","
			+ PROVINCE_FIELD_NAME + "," + PROVINCE_FIELD_DESCRIPTION + ","
			+ REGION_FIELD_ID_NAME + ") VALUES ( %s , '%s' , '%s', %s)";

	// PLACES
	public static String PLACE_TABLE_NAME = "PLACES";
	public static String PLACE_FIELD_ID = "Place_Id";
	public static String PLACE_FIELD_NAME = "Place_Name";
	public static String PLACE_FIELD_DESCRIPTION = "Place_Description";
	public static String PLACE_FIELD_LONG = "Place_Long";
	public static String PLACE_FIELD_LAT = "Place_Lat";
	public static String PLACE_FIELD_RATING = "Rating";
	public static String PLACE_FIELD_PROVINCE_ID = "Province_Id";

	public static String QUERY_GET_PLACE_BY_ID = "SELECT * FROM "
			+ PLACE_TABLE_NAME + " WHERE " + PLACE_FIELD_ID + " = '%s'";
	public static String QUERY_GET_PLACES_BY_PROVINCE_ID = "SELECT * FROM "
			+ PLACE_TABLE_NAME + " WHERE " + PLACE_FIELD_PROVINCE_ID
			+ " = '%s' ";

	public static String QUERY_INSERT_PLACES = "INSERT OR REPLACE INTO "
			+ PLACE_TABLE_NAME + "(" + PLACE_FIELD_ID + "," + PLACE_FIELD_NAME
			+ "," + PLACE_FIELD_DESCRIPTION + "," + PLACE_FIELD_LAT + ","
			+ PLACE_FIELD_LONG + "," + PLACE_FIELD_RATING + ","
			+ PLACE_FIELD_PROVINCE_ID
			+ ") VALUES ( %s , '%s' , '%s', %s, %s, %s, %s)";

	public static String QUERY_GET_ALL_PLACE = "SELECT * FROM "
			+ PLACE_TABLE_NAME;

	public static String QUERY_SEARCH_PLACE = "SELECT * FROM PLACES P, "
			+ "PLACE_ACTIVITY PA, " + "PLACE_SPECIALTY PS, " + "PLACE_TOUR PT "
			+ "WHERE 1 = 1 ";/*
							 * + "P.Place_Id = PA.Place_Id " +
							 * "AND P.Place_Id = PS.Place_Id " +
							 * "AND P.Place_Id = PT.Place_Id ";
							 */
	// FAVORITS

	public static String FAVORIT_TABLE_NAME = "FAVORITS";
	public static String FAVORIT_FIELD = "Place_Id";
	public static String QUERY_GET_FAVORITS = "SELECT * FROM "
			+ FAVORIT_TABLE_NAME;
	public static String QUERY_INSERT_FAVOURITE = "INSERT OR REPLACE INTO "
			+ FAVORIT_TABLE_NAME + "(" + FAVORIT_FIELD + ") VALUES ( %s )";

	// TOURIST FORM

	public static String TOURIST_TABLE_NAME = "TOURIST_FORMS";
	public static String TOURIST_FIELD_ID = "Tour_Id";
	public static String TOURIST_FIELD_NAME = "Tour_Name";
	public static String TOURIST_FIELD_DESCRIPTION = "Tour_Note";

	public static String QUERY_GET_TOURISTS = "SELECT * FROM "
			+ TOURIST_TABLE_NAME;
	public static String QUERY_GET_TOURIST_BY_ID = "SELECT * FROM"
			+ TOURIST_TABLE_NAME + " WHERE " + TOURIST_FIELD_ID + " == '%s'";

	public static String QUERY_INSERT_TOURISTS = "INSERT OR REPLACE INTO "
			+ TOURIST_TABLE_NAME + "(" + TOURIST_FIELD_ID + ","
			+ TOURIST_FIELD_NAME + "," + TOURIST_FIELD_DESCRIPTION
			+ ") VALUES ( %s , '%s' , '%s')";

	// ACTIVITY

	public static String ACTIVITY_TABLE_NAME = "ACTIVITIES";
	public static String ACTIVITY_FIELD_ID = "Activity_Id";
	public static String ACTIVITY_FIELD_NAME = "Activity_Name";
	public static String ACTIVITY_FIELD_NOTE = "Activity_Note";

	public static String QUERY_GET_ACTIVITIES = "SELECT * FROM "
			+ ACTIVITY_TABLE_NAME;
	public static String QUERY_GET_ACTIVITY_BY_ID = "SELECT * FROM "
			+ ACTIVITY_TABLE_NAME + "WHERE " + ACTIVITY_FIELD_ID + " == '%s'";

	public static String QUERY_INSERT_ACTIVITY = "INSERT OR REPLACE INTO "
			+ ACTIVITY_TABLE_NAME + "(" + ACTIVITY_FIELD_ID + ","
			+ ACTIVITY_FIELD_NAME + "," + ACTIVITY_FIELD_NOTE
			+ ") VALUES ( %s , '%s' , '%s')";

	// SPECIALTY
	public static String SPECIALTY_TABLE_NAME = "SPECIALTIES";
	public static String SPECIALTY_FIELD_ID = "Specialty_Id";
	public static String SPECIALTY_FIELD_NAME = "Specialty_Name";
	public static String SPECIALTY_FIELD_NOTE = "Specialty_Note";

	public static String QUERY_GET_SPECIALTIES = "SELECT * FROM "
			+ SPECIALTY_TABLE_NAME;
	public static String QUERY_GET_SPECIALTY_BY_ID = "SELECT * FROM "
			+ SPECIALTY_TABLE_NAME + "WHERE " + SPECIALTY_FIELD_ID + " == '%s'";

	public static String QUERY_INSERT_SPECIALTY = "INSERT OR REPLACE INTO "
			+ SPECIALTY_TABLE_NAME + "(" + SPECIALTY_FIELD_ID + ","
			+ SPECIALTY_FIELD_NAME + "," + SPECIALTY_FIELD_NOTE
			+ ") VALUES ( %s , '%s' , '%s')";

	// PLACE_ACTIVITY
	public static String PLACE_ACTIVITY_TABLE_NAME = "PLACE_ACTIVITY";
	public static String QUERY_INSERT_PLACE_ACTIVITY = "INSERT OR REPLACE INTO "
			+ PLACE_ACTIVITY_TABLE_NAME
			+ "("
			+ PLACE_FIELD_ID
			+ ","
			+ ACTIVITY_FIELD_ID + ") VALUES(%s,%s)";

	// PLACE_SPECIALTY
	public static String PLACE_SPECIALTY_TABLE_NAME = "PLACE_SPECIALTY";
	public static String QUERY_INSERT_PLACE_SPECIALTY = "INSERT OR REPLACE INTO "
			+ PLACE_SPECIALTY_TABLE_NAME
			+ "("
			+ PLACE_FIELD_ID
			+ ","
			+ SPECIALTY_FIELD_ID + ") VALUES(%s,%s)";

	// PLACE_TOUR
	public static String PLACE_TOUR_TABLE_NAME = "PLACE_TOUR";
	public static String QUERY_INSERT_PLACE_TOUR = "INSERT OR REPLACE INTO "
			+ PLACE_TOUR_TABLE_NAME + "(" + PLACE_FIELD_ID + ","
			+ TOURIST_FIELD_ID + ") VALUES(%s,%s)";

	// Convert String
	@SuppressLint("NewApi")
	public static String convertUTF8String(String _originalString) {
		Pattern ACCENTS_PATTERN = Pattern
				.compile("[\\p{InCombiningDiacriticalMarks}]+");
		String resultString = ACCENTS_PATTERN.matcher(
				Normalizer.normalize(_originalString, Normalizer.Form.NFD))
				.replaceAll("");
		return resultString.replace('Ð', 'D').replace('đ', 'd');
	}

	public static boolean isContain(String _originalString, String subString) {
		return convertUTF8String(_originalString.toLowerCase()).contains(
				convertUTF8String(subString.toLowerCase()));
	}

	// Convert byte array to image
	public static Drawable byteArray2Drawable(byte[] b) {
		Drawable image = null;
		image = new BitmapDrawable(
				BitmapFactory.decodeByteArray(b, 0, b.length));
		return image;
	}

	public static byte[] drawable2ByteArray(Drawable drawable) {
		Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		byte[] bitmapdata = stream.toByteArray();
		return bitmapdata;
	}

	public static String byteArray2String(byte[] buf) {
		return Base64.encodeToString(buf, Base64.DEFAULT);
	}

	public static byte[] string2ByteArray(String str) {
		byte[] image = Base64.decode(str, Base64.DEFAULT);
		return image;
	}

	// Check connection

	public static boolean isConnection(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifiNetwork = cm
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiNetwork != null && wifiNetwork.isConnected())
			return true;
		NetworkInfo mobileNetwork = cm
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mobileNetwork != null && mobileNetwork.isConnected())
			return true;
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected())
			return true;
		return false;
	}

	// reduce image size

	public static Drawable reduceImage(Drawable image, int width, int height) {
		Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
		Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap,
				width, height, true));
		return d;
	}

	// google data
	public static final String TAG_STATUS = "status";
	public static final String TAG_ROUTES = "routes";
	public static final String TAG_LEGS = "legs";
	public static final String TAG_WARNINGS = "warnings";
	public static final String TAG_DISTANCE = "distance";
	public static final String TAG_TEXT = "text";
	public static final String TAG_END_ADDRESS = "end_address";
	public static final String TAG_END_LOCATION = "end_location";
	public static final String TAG_LAT = "lat";
	public static final String TAG_LNG = "lng";
	public static final String TAG_STEPS = "steps";
	public static final String TAG_VALUE = "value";
	public static final String TAG_DURATION = "duration";
	public static final String TAG_START_ADDRESS = "start_address";
	public static final String TAG_START_LOCATION = "start_location";
	public static final String TAG_HTML_INSTRUCTIONS = "html_instructions";
	public static final String TAG_POLYLINE = "polyline";
	public static final String TAG_POINTS = "points";
	public static final String TAG_TRAVE_MODE = "travel_mode";
	public static final String TAG_OVERVIEW_POLYLINE = "overview_polyline";
	public static final String TAG_SUMMARY = "summary";
	public static final String TAG_RESULTS = "results";
	public static final String TAG_FORMATTED_ADDRESS = "formatted_address";
	public static final String TAG_GEOMETRY = "geometry";
	public static final String TAG_LOCATION = "location";
	public static final String TAG_TYPES = "types";
	public static final String TAG_NAME = "name";
	public static final String TAG_LADDR = "laddr";
	public static final String TAG_LATLNG = "latlng";
	public static final String TAG_OVERLAYS = "overlays";
	public static final String TAG_ADDRESS_COMPONENTS = "address_components";
	public static final String TAG_LONG_NAME = "long_name";

	public static final String DIRECTIONLIST = "direction_list";
	public static final String START = "start";
	public static final String END = "end";
	public static String MY_LOCATION = "mylocation";
	public static String PLACE_WANT_SEARCH = "place";

	public static final String LOCATION_KEY = "key";
	public static final String LOCATIONLIST = "location_list";

	public static Integer PERPAGE;
	public static Integer TOTAL;
	public static Integer PAGE;
	public static Integer TOTALPAGE;

	private static final double DEGREES_TO_RADIANS = (double) (180 / Math.PI);
	private final static double EARTHS_RADIUS = 6378100;

	private static double calclateArc(double aLat, double aLong, double bLat,
			double bLong) {
		double aLatRad = aLat / DEGREES_TO_RADIANS;
		double aLongRad = aLong / DEGREES_TO_RADIANS;
		double bLatRad = bLat / DEGREES_TO_RADIANS;
		double bLongRad = bLong / DEGREES_TO_RADIANS;

		// Calculate the length of the arc that subtends point a and b
		double t1 = Math.cos(aLatRad) * Math.cos(aLongRad) * Math.cos(bLatRad)
				* Math.cos(bLongRad);
		double t2 = Math.cos(aLatRad) * Math.sin(aLongRad) * Math.cos(bLatRad)
				* Math.sin(bLongRad);
		double t3 = Math.sin(aLatRad) * Math.sin(bLatRad);
		double tt = Math.acos(t1 + t2 + t3);
		return tt;
	}

	public static double calculateDistance(LatLng from, LatLng to) {
		return calclateArc(from.latitude, from.longitude, to.latitude,
				to.longitude) * EARTHS_RADIUS;
	}

}
